package com.jiutian.db.util;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jiutian.factory.HibernateSessionFactory;

public class HibernateUtil {
    public static List excute(String sql) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();

            SQLQuery sqlquery = session.createSQLQuery(sql);

            return sqlquery.list();
        } catch (Exception ex) {
            ex.printStackTrace();

            return null;
        } finally {
            session.close();
        }
    }

    public static Object get1(String sql) {
        Session session = null;

        try {
            session = HibernateSessionFactory.getSession();

            SQLQuery sqlquery = session.createSQLQuery(sql);

            return sqlquery.uniqueResult();
        } catch (Exception ex) {
            ex.printStackTrace();

            return null;
        } finally {
            session.close();
        }
    }

    public static boolean saveObj(Object obj) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateSessionFactory.getSession();
            tx = session.beginTransaction();
            session.save(obj);
            tx.commit();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
            return false;
        } finally {
            session.close();
        }
    }
}
